package com.jeasonzhao.report.engine.impl;

import com.jeasonzhao.report.engine.ReportUserInfo;
import com.jeasonzhao.report.exceptions.ReportRuntimeException;
import com.jeasonzhao.report.model.Report;
import com.jeasonzhao.report.model.ReportParam;
import com.jeasonzhao.report.exceptions.ReportParameterException;

public class HashParameterValueCollector extends AbstractParameterValueCollector
{
    private java.util.Hashtable<String,Object> m_values = new java.util.Hashtable<String,Object>();
    private int pageSize = 0,pageNo = 0;
    private String renderKey = null;

    public HashParameterValueCollector()
    {
    }

    public int getPageSize(Report report)
        throws ReportRuntimeException
    {
        return null==report?-1:this.pageSize;
    }

    public int getPageNo(Report report)
        throws ReportRuntimeException
    {
        return null==report?0:this.pageNo;
    }

    public String getRenderKey()
    {
        return this.renderKey;
    }

    public void collectDynQuerySettings(Report report)
        throws ReportRuntimeException
    {
    }

    public int getPageNo()
    {
        return pageNo;
    }

    public int getPageSize()
    {
        return pageSize;
    }

    public void setPageNo(int pageNo)
    {
        this.pageNo = pageNo;
    }

    public void setPageSize(int pageSize)
    {
        this.pageSize = pageSize;
    }

    public void setRenderKey(String renderKey)
    {
        this.renderKey = renderKey;
    }

    public HashParameterValueCollector addValue(String key,Object v)
    {
        if(null == key)
        {
            return this;
        }
        key = key.toLowerCase();
        if(null == v)
        {
            this.m_values.remove(key);
        }
        else
        {
            this.m_values.put(key,v);
        }
        return this;
    }

    @Override
    protected boolean getParameterValue(Report report,ReportUserInfo user,ReportParam parameter)
        throws ReportParameterException
    {
        if(null == parameter|| null==report)
        {
            return false;
        }
        else
        {
            Object v = m_values.get(parameter.getParam().toLowerCase());
            if(null != v)
            {
                parameter.setValue(v);
            }
        }
        return true;
    }

}
